package com.gitee.wsl.compose.ui.textfield.filter

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import com.gitee.wsl.compose.ui.base.TextField


class FilterGMT() : BaseFieldFilter() {

    override fun onFilter(
        inputTextFieldValue: TextFieldValue,
        lastTextFieldValue: TextFieldValue
    ): TextFieldValue {
        return filterInputGMT(inputTextFieldValue, lastTextFieldValue)
    }

    // GMT+08:00
    private fun filterInputGMT(inputTextFieldValue: TextFieldValue, lastTextFiled: TextFieldValue): TextFieldValue {
        val inputValue = inputTextFieldValue.text
        val newValue = StringBuffer()
        val standString = StringBuffer("GMT+08:00")
        var legalIndex = 0
        var pos = inputTextFieldValue.selection.end

        for (c in inputValue) {
            when (legalIndex) {
                0, 1, 2 -> {
                    if (c.uppercase() == standString[legalIndex].uppercase()) {
                        newValue.append(c.uppercase())
                        legalIndex++
                    }
                    else {
                        pos--
                    }
                }
                3 -> {
                    if (c == '+' || c == '-') {
                        newValue.append(c)
                        legalIndex++
                    }
                    else {
                        pos--
                    }
                }
                4 -> {
                    if (c.isDigit()) {
                        newValue.append(c)
                        legalIndex++
                    }
                    else {
                        pos--
                    }
                }
                5 -> {
                    if (c.isDigit()) {
                        newValue.append(c)
                        legalIndex++
                    }
                    if (c == ':') {
                        newValue.insert(4, "0")
                        newValue.append(":")
                        legalIndex++
                        pos++
                    }
                }
                6 -> {
                    if (c == ':') {
                        newValue.append(":")
                        legalIndex++
                    }
                }
                7 -> {
                    if (c.isDigit() && (c.code - '0'.code) <= 5) {
                        newValue.append(c)
                        legalIndex++
                    }
                    else {
                        pos--
                    }
                }
                8 -> {
                    if (c.isDigit()) {
                        newValue.append(c)
                        legalIndex++
                    }
                    else {
                        pos--
                    }
                }
            }
            if (legalIndex > 8) break
        }

        return inputTextFieldValue.copy(text = newValue.toString(), selection = TextRange(pos))
    }
}

@Composable
fun TextField.Filter.gmt() = remember { FilterGMT() }